package com.cskaoyan.service.wx.zjh;

import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.pojo.MarketAddress;
import com.cskaoyan.module.pojo.MarketAddressExample;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.module.vo.wx.zjh.AddressListVO;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: 周佳豪
 * @Date: 2022/04/10/22:01
 */
@Service
public class AddressServiceImpl implements AddressService {
    BaseRespVo baseRespVo = new BaseRespVo();

    MarketAddressExample example = new MarketAddressExample();
    MarketAddressExample.Criteria criteria = example.createCriteria();

    @Autowired
    MarketAddressMapper addressMapper;


    @Override
    public BaseRespVo selectAddress(Integer id) {

        criteria.andUserIdEqualTo(id);

        List<MarketAddress> marketAddresses = addressMapper.selectByExample(example);

        AddressListVO addressListVO = new AddressListVO(marketAddresses.size(), 1, marketAddresses.size(), 1, marketAddresses);

        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(addressListVO);
        return baseRespVo;
    }

    @Override
    public BaseRespVo detailAddress(Integer id) {
        MarketAddress marketAddress = addressMapper.selectByPrimaryKey(id);
        baseRespVo.setData(marketAddress);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    @Override
    public BaseRespVo saveAddress(MarketAddress address) {
        MarketAddress marketAddress = addressMapper.selectByPrimaryKey(address.getId());
        if (marketAddress != null) {
            addressMapper.updateByPrimaryKey(address);
            baseRespVo.setData(address.getId());
            baseRespVo.setErrmsg("成功");
            baseRespVo.setErrno(0);

            return baseRespVo;
        }else {
            addressMapper.insert(address);

            baseRespVo.setData(address.getId());
            baseRespVo.setErrmsg("成功");
            baseRespVo.setErrno(0);

            return baseRespVo;
        }
    }

    @Override
    public BaseRespVo deleteAddress(Integer id) {
        int i = addressMapper.deleteByPrimaryKey(id);
        if (i == 1) {
            baseRespVo.setErrno(0);
            baseRespVo.setErrmsg("成功");
            baseRespVo.setData(null);
        }
        return baseRespVo;
    }
}
